package com.yh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.model.Role;
import com.yh.model.UserInfo;
import com.yh.service.UserService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.security.RolesAllowed;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;

    @RolesAllowed({"ADMIN","GroupManger"})
    @RequestMapping("findAll")
    public String findAll(@RequestParam(name = "page", defaultValue = "1") Integer page,
                          @RequestParam(name = "size", defaultValue = "5") Integer size,
                          Model model) {
        PageHelper.startPage(page, size);
        List<UserInfo> userInfos = userService.findAll();
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfos);

        model.addAttribute("pageInfo", pageInfo);
        return "user-list";
    }

    @RequestMapping("save")
    public String save(UserInfo userInfo) {
        userService.save(userInfo);
        return "redirect:findAll";
    }

    ///user/findById(id=
    /**
     * 根据用户id查询用户详情(用户 角色 权限)
     * @param id 用户id
     * @param model 封装模型数据
     * @return 渲染页面
     */
    @GetMapping("findById")
    public String findById(@RequestParam("id") Integer id, Model model) {
        UserInfo userInfo = userService.findById(id);
        model.addAttribute("user", userInfo);
        return "user-show";
    }

    //user/findUserByIdAndAllRole?id=1
    @GetMapping("findUserByIdAndAllRole")
    public String findUserByIdAndAllRole(@RequestParam("id") Integer id, Model model) {
        //用户可添加的角色集合
        List<Role> roles = userService.findUserByIdAndAllRole(id);
        model.addAttribute("userId",id); //添加角色时需要用户id
        model.addAttribute("roleList", roles); //添加角色集合
        return "user-role-add";
    }


    //user/addRoleToUser
    @RequestMapping("addRoleToUser")
    public String addRoleToUser(@RequestParam("userId") Integer userId,
                                @RequestParam("ids") Integer[] ids) {
        userService.addRoleToUser(userId, ids);
        return "redirect:findById?id="+userId;
    }

    //删除指定用户(并且删除关系表中的关联数据)
    //deleteUser?id=6
    @RequestMapping("deleteUser")
    public String deleteUser(@RequestParam("id") Integer id) {
        userService.deleteUser(id);
        return "redirect:findAll";
    }

    /**
     * 获取登录的用户名
     * @return
     */
    @RequestMapping("getUsername")
    @ResponseBody
    public String getUsername() {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principal.getUsername();
        return username;
    }
}
